package com.caineng.zhaobiao.zbapi.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.caineng.zhaobiao.zbapi.controller.paramVo.CaiCommentVo;
import com.caineng.zhaobiao.zbapi.dao.CaiCommentDao;
import com.caineng.zhaobiao.zbapi.domain.CaiBaseUserComment;
import com.caineng.zhaobiao.zbapi.service.CaiCommentService;
import com.caineng.zhaobiao.zbapi.utils.AjaxResult;
import com.caineng.zhaobiao.zbapi.utils.enums.USER_ACTION;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 招标评论服务类
 * @author liandyao
 * @date 2022/7/18 15:46
 */
@Slf4j
@Api(tags = "招标评论服务")
@RestController
@RequestMapping("/zbapi/comment")
public class CaiCommentController {

    @Autowired
    private CaiCommentService commentService;

    @Autowired
    private CaiCommentDao commentDao;

    @GetMapping("/findAll")
    @ApiOperation(value = "查询招标和公司的评论信息")
    public AjaxResult findAll(CaiCommentVo qp){
        log.info(JSONUtil.toJsonStr(qp));
        Pageable page = PageRequest.of(qp.getCurPage(),qp.getPageSize(), Sort.Direction.DESC,"createTime");
        CaiBaseUserComment zbParam = new CaiBaseUserComment();
        zbParam.setBusId(qp.getBusId());
        zbParam.setType(qp.getType());
        //用户操作类型
        zbParam.setType(USER_ACTION.comment.name());
        Example<CaiBaseUserComment> ex = Example.of(zbParam);
        Page<CaiBaseUserComment> records = commentDao.findAll(ex,page);
        log.info(records.getContent().toString());
        return AjaxResult.success("成功",records);

    }

    @GetMapping("/findAll2")
    @ApiOperation(value = "查询招标和公司的评论信息2")
    public AjaxResult findAll2(){
        List<CaiBaseUserComment> records = commentDao.findAll(Sort.by("createTime"));
        log.info(records.toString());
        return AjaxResult.success("成功",records);

    }


    @GetMapping("/findFavorite")
    @ApiOperation(value = "关联查询我的收藏信息")
    public AjaxResult findFavorite(){
        String userId = StpUtil.getLoginIdAsString();
        List<JSONObject> list = commentService.lookupComment(userId,USER_ACTION.fav.name());
        return AjaxResult.success("成功",list);
    }


    @PostMapping("/addComment")
    @ApiOperation(value = "添加评论")
    public AjaxResult addComment(CaiCommentVo vo){
        String userId = StpUtil.getLoginIdAsString();
        commentService.addComment(vo.getBusId(),vo.getContent(),userId);
        return AjaxResult.success("成功");
    }

    @PostMapping("/addCommentDigg")
    @ApiOperation(value = "评论点赞")
    public AjaxResult addCommentDigg(String id){

        Optional<CaiBaseUserComment> byId = commentDao.findById(id);
        CaiBaseUserComment comment = byId.get();
        comment.setDiggCount(comment.getDiggCount()==null?1:comment.getDiggCount()+1);
        commentDao.save(comment);

        return AjaxResult.success("成功");
    }


}
